﻿
function jqGrid_ResizeToFit(gridId) {

    try { // PROTECT THE GRID RESIZE IN CASE IT IS NOT AVAIALBLE

        var gridBox = $("#gbox_" + gridId);

        if (!gridBox.is(":visible")) { return; } // IF THE GRID IS NOT SET TO VISIBLE, DO NOT RESIZE IT


        // SET WIDTH TO PARENT WIDTH, MINUS 2 FOR BORDERS 

        var adjustedWidth = gridBox.parent().innerWidth() - 2; // EXCLUDE BORDER WIDTHS

        $("#" + gridId).setGridWidth(adjustedWidth);


        // SET GRID HEIGHT

        var adjustedHeight = gridBox.parent().height();

        // IF GRID HAS VISIBLE TITLE BAR, ACCOUNT FOR ITS HEIGHT

        if ($("#gview_" + gridId + " .ui-jqgrid-titlebar :visible").length != 0) {

            adjustedHeight = adjustedHeight - $("#gview_" + gridId + " .ui-jqgrid-titlebar").outerHeight(); // REDUCE BY TITLE BAR SIZE

        }

        adjustedHeight = adjustedHeight - $("#gview_" + gridId + " .ui-jqgrid-hdiv").outerHeight();  // REDUCE BY HEADER SIZE

        // IF GRID HAS PAGER, ACCOUNT FOR ITS HEIGHT

        if ($("#" + gridId + "Pager").length != 0) {

            adjustedHeight = adjustedHeight - $("#" + gridId + "Pager").outerHeight(); // REDUCE BY PAGER SIZE 

        }

        adjustedHeight = adjustedHeight - 2; // EXCLUDE BORDER

        $("#" + gridId).setGridHeight(adjustedHeight);

    }

    catch (exception) { /* DO NOTHING */ }

    return;

}


function jqGrid_ResizeToFitWidth(gridId) {

    try { // PROTECT THE GRID RESIZE IN CASE IT IS NOT AVAIALBLE

        var gridBox = $("#gbox_" + gridId);

        if (!gridBox.is(":visible")) { return; } // IF THE GRID IS NOT SET TO VISIBLE, DO NOT RESIZE IT


        // SET WIDTH TO PARENT WIDTH, MINUS 2 FOR BORDERS 

        var adjustedWidth = gridBox.parent().innerWidth() - 2; // EXCLUDE BORDER WIDTHS

        $("#" + gridId).setGridWidth(adjustedWidth);

    }

    catch (exception) { /* DO NOTHING */ }

    return;

}

function jqGrid_ResizeSubgridWidthToFit(gridId) {

    try { // PROTECT THE GRID RESIZE IN CASE IT IS NOT AVAIALBLE

        var gridBox = $("#gbox_" + gridId + "_t");

        if (!gridBox.is(":visible")) { return; } // IF THE GRID IS NOT SET TO VISIBLE, DO NOT RESIZE IT


        // SET WIDTH TO PARENT WIDTH, MINUS 2 FOR BORDERS 

        var adjustedWidth = gridBox.parent().innerWidth() - 2; // EXCLUDE BORDER WIDTHS

        $("#" + gridId + "_t").setGridWidth(adjustedWidth);

    }

    catch (exception) { /* DO NOTHING */ }

    return;

}

function jqGrid_RemoveCorners(gridId) {

    $("#gbox_" + gridId + ".ui-corner-all").removeClass("ui-corner-all");

    $("#" + gridId + "Pager.ui-corner-bottom").removeClass("ui-corner-bottom");

    return;

}

function jqGrid_Formatters_JsonDotNetDate_Short(cellValue, options, rowObject) {

    var formattedDate = "";

    if (cellValue.length > 2) {

        formattedDate = cellValue.substr(1, cellValue.length - 2); // TRIM LEADING AND ENDING /'s

        formattedDate = (eval("new " + formattedDate)); // CONVERT TO JAVASCRIPT DATE OBJECT 

        formattedDate = $.datepicker.formatDate("yy-mm-dd", formattedDate);

    }

    return formattedDate;

}


function jqGrid_Formatters_JsonDotNetDate_Long(cellValue, options, rowObject) {

    var formattedDate = "";

    if ((cellValue != null) && (cellValue != undefined)) {

        if (cellValue.length > 2) {

            formattedDate = cellValue.substr(1, cellValue.length - 2); // TRIM LEADING AND ENDING /'s

            formattedDate = (eval("new " + formattedDate)); // CONVERT TO JAVASCRIPT DATE OBJECT 

            formattedDate = $.datepicker.formatDate("yy-mm-dd", formattedDate) + " " + formattedDate.toTimeString().substr(0, 8);

        }

    }

    return formattedDate;

}